/-boot
/-docs
/-editor
/-files
/-files-old
/-imports
/-layout
/-shell
/-storage ...
/-storage/attached ...
/-storage/attached/api
/-storage/attached/dom
/-storage/attached/indexedDB
/-storage/attached/localStorage ...
DetectStorage.ts
LoadStorage.ts
StorageAccess.ts
StorageDetect.ts
UpdateStorage.ts
/-storage/attached/webSQL
/-tests
/-tests/files
/-tests/storage
/-tests/storage/attached
AttachedStorageTests.ts
AttachedStorageTestsNew.ts
DomStorageTests.ts
IndexedDBStorageTests.ts
LocalStorageStorageTests.ts
WebSQLStorageTests.ts
TestCase.html
TestCase.ts
TestPage.css
TestPage.html
TestPage.ts
_sampleTests.ts
teapo-tests.html
teapo-tests.ts
/-typings
Dom.ts
TypeScriptService.ts
functions.ts
ko.ts
nteapo.html
persistence.api.ts
persistence.ts
shell.ts
teapo.html
teapo.ts
try.html
try.js
94
 
1
module teapo.storage.attached.localStorage { 
2
​
3
  export class StorageAccess implements attached.StorageAccess { 
4
  
5
    private _nameCache: { [name: string]: { [name: string]: string; }; } = {};
6
​
7
    constructor(
8
      private _prefix: string,
9
      private _localStorage: Storage,
10
      private _editedKey: string) {
11
    }
12
​
13
    update(
14
      byFullPath: PropertiesByFullPath,
15
      timestamp: number,
16
      callback: (error: Error) => void): void {
17
      for (var fullPath in byFullPath) if (byFullPath.hasOwnProperty(fullPath)) {
18
        var propBag = byFullPath[fullPath];
19
        if (!propBag) {
20
          var cacheLine = this._nameCache[fullPath];
21
          if (!cacheLine) continue;
22
​
23
          for (var k in cacheLine) if (cacheLine.hasOwnProperty(k)) {
24
            var key = cacheLine[k];
25
            this._localStorage.removeItem(key);
26
          }
27
​
28
          delete this._nameCache[fullPath];
29
        }
30
        else {
31
          var cacheLine = this._nameCache[fullPath] || (this._nameCache[fullPath] = {});
32
          for (var k in propBag) if (propBag.hasOwnProperty(k)) {
33
            var key = cacheLine[k] || (cacheLine[k] = this._prefix + fullPath + '*' + k)
34
            var value = propBag[k];
35
​
36
            if (value === null || typeof value === 'undefined') {
37
              this._localStorage.removeItem(key);
38
              delete cacheLine[k];
39
            }
40
            else {
41
              this._localStorage.setItem(key, value);
42
            }
43
          }
44
        }
45
      }
46
​
47
      this._updateEdited(timestamp);
48
​
49
      callback(null);
50
    }
51
​
52
    read(
53
      fullPaths: string[],
54
      callback: (error: Error, byFullPath: PropertiesByFullPath) => void): void {
55
​
56
      var allPaths = fullPaths ? false : true;
57
​
58
      var byFullPath: PropertiesByFullPath = {};
59
      if (!allPaths) {
60
        for (var i = 0; i < fullPaths.length; i++) {
61
          byFullPath[fullPaths[i]] = {};
62
        }
63
      }
64
​
65
      for (var i = 0; i < this._localStorage.length; i++) {
66
        var key = this._localStorage.key(i);
67
        if (!startsWith(key, this._prefix)) continue;
68
​
69
        var starPos = key.indexOf('*', this._prefix.length);
70
        if (starPos < 0) continue;
71
​
72
        var fullPath = key.slice(this._prefix.length, starPos);
73
        var propertyName = key.slice(starPos + 1);
74
        var value = this._localStorage.getItem(key);
75
​
76
        if (!fullPaths && !byFullPath[fullPath])
77
          continue; // they are not interested in this one
78
​
79
        if (!byFullPath[fullPath])
80
          byFullPath[fullPath] = {};
81
​
82
        byFullPath[fullPath][propertyName] = value;
83
      }
84
​
85
      callback(null, byFullPath);
86
    }
87
    
88
    private _updateEdited(editedUTC: number) {
89
      this._localStorage.setItem(this._editedKey, <any>editedUTC);
90
    }
91
​
92
  }
93
​
94
}
59:10